package com.cn.qgx428.lazydiary.ui.activity

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.RecordHelper
import com.cn.qgx428.lazydiary.ui.fragment.ArticleFragment
import com.cn.qgx428.lazydiary.ui.fragment.FunctionFragment
import com.cn.qgx428.lazydiary.ui.fragment.MineFragment
import com.cn.qgx428.lazydiary.ui.fragment.TimeLineFragment
import kotlinx.android.synthetic.main.activity_home.*
import android.content.Context.CONNECTIVITY_SERVICE
import android.net.ConnectivityManager
import android.net.NetworkInfo
import com.cn.qgx428.lazydiary.Utils.SPUtil


class HomeActivity : BaseActivity(){

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        initFragmentAndTab()
    }

    /**
     * 装载fragment进入viewPager
     */
    private fun initFragmentAndTab() {
        val timeLineFragment = TimeLineFragment()
        val functionFragment = FunctionFragment()
        val mineFragment = MineFragment()

        val fragments:List<Fragment> = listOf(timeLineFragment,
              functionFragment,mineFragment)
        home_viewPager.adapter = MFAdapter(supportFragmentManager, fragments)
        home_viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                changeTabStyle(position)
            }

        })

        home_tabLayout.setupWithViewPager(home_viewPager)
        changeTabStyle(0)
    }

    val titleUnfocus:List<Int> = listOf(R.drawable.ic_ufo_white,
            R.drawable.ic_add_white_, R.drawable.ic_mine_white)
    val titleFocus: List<Int> = listOf(R.drawable.ic_ufo_blue,
            R.drawable.ic_add_blue, R.drawable.ic_mine_blue)
    var titleNames: List<String> = listOf("时间线",  "记录", "我的")

    /**
     * 改变导航栏样式, 显出聚焦的图标和文字
     */
    private fun changeTabStyle(position:Int){
        for (i in 0..(titleUnfocus.size-1)){
            home_tabLayout.getTabAt(i)?.setIcon(titleUnfocus.get(i))
            home_tabLayout.getTabAt(i)?.setText(titleNames.get(i))
        }
        home_tabLayout.getTabAt(position)?.setIcon(titleFocus.get(position))
    }

    /**
     *
     *
     * 暴露出去方法
     *
     *
     */
    public fun checkLoginAndNetWork(): Boolean{
        if (isNetworkAvalible() && SPUtil.checkUserLogin())
            return true
        return false
    }

    private fun isNetworkAvalible(): Boolean{
        // 获得网络状态管理器
        val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        if (connectivityManager == null) {
            return false
        } else {
            // 建立网络数组
            val net_info = connectivityManager.allNetworkInfo

            if (net_info != null) {
                for (i in net_info.indices) {
                    // 判断获得的网络状态是否是处于连接状态
                    if (net_info[i].state === NetworkInfo.State.CONNECTED) {
                        return true
                    }
                }
            }
        }
        return false
    }

    /**
     *
     *
     *
     *
     *
     * 分界线
     *
     *
     *
     *
     *
     */
    class MFAdapter constructor(fm: FragmentManager, fragments:List<Fragment>): FragmentPagerAdapter(fm) {

        lateinit var fragments:List<Fragment>

        init {
            this.fragments = fragments
        }

        override fun getItem(position: Int) = fragments.get(position)
        override fun getCount() = fragments.size
    }
}
